package zzs.generalframework.console.core.parameter.commonparameter.dictionary.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.parameter.commonparameter.dictionary.DictionaryInfo;
import zzs.generalframework.common.core.parameter.commonparameter.dictionary.DictionaryPO;
import zzs.generalframework.console.core.parameter.commonparameter.dictionary.dao.IDictionaryDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.parameter.commonparameter.dictionary.dao.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.parameter.commonparameter.dictionary.dao.impl.dictionaryDaoImpl")
public class DictionaryDaoImpl implements IDictionaryDao
{
	private Logger logger = LoggerFactory.getLogger(DictionaryDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(DictionaryPO dictionaryPO)
	{
		logger.info("====插入数据字典数据开始====");
		logger.debug("====插入数据字典数据,入参:DictionaryPO=[{}]====", dictionaryPO);
		entityManager.persist(dictionaryPO);
		logger.debug("====插入数据字典数据,出参:boolean=[{}]====", true);
		logger.info("====插入数据字典数据结束====");
		return true;
	}

	@Override
	public boolean update(DictionaryPO dictionaryPO)
	{
		logger.info("====更新数据字典数据开始====");
		logger.debug("====更新数据字典数据,入参:DictionaryPO=[{}]====", dictionaryPO);
		entityManager.merge(dictionaryPO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新数据字典数据结束====");
		return true;
	}

	@Override
	public List<DictionaryPO> select(DictionaryInfo dictionaryInfo)
	{
		logger.info("====查询数据字典数据开始====");
		logger.debug("====查询数据字典数据,入参:DictionaryInfo=[{}]====", dictionaryInfo);
		List<DictionaryPO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from DictionaryPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(dictionaryInfo.getDictionaryID()))
		{
			sqlBuffer.append(" and a.dictionaryID=?");
			parameter.putParameterObject(dictionaryInfo.getDictionaryID());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryInfo.getDictionaryCode()))
		{
			sqlBuffer.append(" and a.dictionaryCode=?");
			parameter.putParameterObject(dictionaryInfo.getDictionaryCode());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryInfo.getItemCode()))
		{
			sqlBuffer.append(" and a.itemCode=?");
			parameter.putParameterObject(dictionaryInfo.getItemCode());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryInfo.getItemName()))
		{
			sqlBuffer.append(" and a.itemName=?");
			parameter.putParameterObject(dictionaryInfo.getItemName());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryInfo.getDictTypeCode()))
		{
			sqlBuffer.append(" and a.dictTypeCode=?");
			parameter.putParameterObject(dictionaryInfo.getDictTypeCode());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryInfo.getDictTypeName()))
		{
			sqlBuffer.append(" and a.dictTypeName=?");
			parameter.putParameterObject(dictionaryInfo.getDictTypeName());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryInfo.getCreateUser()))
		{
			sqlBuffer.append(" and a.createUser=?");
			parameter.putParameterObject(dictionaryInfo.getCreateUser());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryInfo.getUpdateUser()))
		{
			sqlBuffer.append(" and a.updateUser=?");
			parameter.putParameterObject(dictionaryInfo.getUpdateUser());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryInfo.getExtendedUsage4()))
		{
			sqlBuffer.append(" and a.extendedUsage4=?");
			parameter.putParameterObject(dictionaryInfo.getExtendedUsage4());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryInfo.getExtendedUsage3()))
		{
			sqlBuffer.append(" and a.extendedUsage3=?");
			parameter.putParameterObject(dictionaryInfo.getExtendedUsage3());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryInfo.getExtendedUsage2()))
		{
			sqlBuffer.append(" and a.extendedUsage2=?");
			parameter.putParameterObject(dictionaryInfo.getExtendedUsage2());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryInfo.getExtendedUsage1()))
		{
			sqlBuffer.append(" and a.extendedUsage1=?");
			parameter.putParameterObject(dictionaryInfo.getExtendedUsage1());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryInfo.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(dictionaryInfo.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryInfo.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(dictionaryInfo.getRecordStatus());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询数据字典数据,出参:List<DictionaryPO>=[{}]====", returnList);
		logger.info("====查询数据字典数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(DictionaryPO dictionaryPO)
	{
		logger.info("====删除数据字典数据开始====");
		logger.debug("====删除数据字典数据,入参:DictionaryPO=[{}]====", dictionaryPO);
		entityManager.remove(dictionaryPO);
		logger.debug("====删除数据字典数据,出参:boolean=[{}]====", true);
		logger.info("====删除数据字典数据结束====");
		return true;
	}

	@Override
	public Long count(DictionaryPO dictionaryPO)
	{
		logger.info("====统计数据字典数据开始====");
		logger.debug("====统计数据字典数据,入参:DictionaryPO=[{}]====", dictionaryPO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from DictionaryPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(dictionaryPO.getDictionaryID()))
		{
			sqlBuffer.append(" and a.dictionaryID=?");
			parameter.putParameterObject(dictionaryPO.getDictionaryID());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryPO.getDictionaryCode()))
		{
			sqlBuffer.append(" and a.dictionaryCode=?");
			parameter.putParameterObject(dictionaryPO.getDictionaryCode());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryPO.getItemCode()))
		{
			sqlBuffer.append(" and a.itemCode=?");
			parameter.putParameterObject(dictionaryPO.getItemCode());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryPO.getItemName()))
		{
			sqlBuffer.append(" and a.itemName=?");
			parameter.putParameterObject(dictionaryPO.getItemName());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryPO.getDictTypeCode()))
		{
			sqlBuffer.append(" and a.dictTypeCode=?");
			parameter.putParameterObject(dictionaryPO.getDictTypeCode());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryPO.getDictTypeName()))
		{
			sqlBuffer.append(" and a.dictTypeName=?");
			parameter.putParameterObject(dictionaryPO.getDictTypeName());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryPO.getCreateUser()))
		{
			sqlBuffer.append(" and a.createUser=?");
			parameter.putParameterObject(dictionaryPO.getCreateUser());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryPO.getUpdateUser()))
		{
			sqlBuffer.append(" and a.updateUser=?");
			parameter.putParameterObject(dictionaryPO.getUpdateUser());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryPO.getExtendedUsage4()))
		{
			sqlBuffer.append(" and a.extendedUsage4=?");
			parameter.putParameterObject(dictionaryPO.getExtendedUsage4());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryPO.getExtendedUsage3()))
		{
			sqlBuffer.append(" and a.extendedUsage3=?");
			parameter.putParameterObject(dictionaryPO.getExtendedUsage3());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryPO.getExtendedUsage2()))
		{
			sqlBuffer.append(" and a.extendedUsage2=?");
			parameter.putParameterObject(dictionaryPO.getExtendedUsage2());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryPO.getExtendedUsage1()))
		{
			sqlBuffer.append(" and a.extendedUsage1=?");
			parameter.putParameterObject(dictionaryPO.getExtendedUsage1());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryPO.getOrderNumber()))
		{
			sqlBuffer.append(" and a.orderNumber=?");
			parameter.putParameterObject(dictionaryPO.getOrderNumber());
		}
		if (StringUtils.isNotNullAndBlank(dictionaryPO.getRecordStatus()))
		{
			sqlBuffer.append(" and a.recordStatus=?");
			parameter.putParameterObject(dictionaryPO.getRecordStatus());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计数据字典数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计数据字典数据结束====");
		return returnLong;
	}

}
